<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>清空会话数据</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        pre {
            background-color: #fff;
            padding: 15px;
            border-radius: 4px;
            border: 1px solid #ddd;
            white-space: pre-wrap;
            word-wrap: break-word;
            max-height: 300px;
            overflow-y: auto;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 4px;
            margin-top: 20px;
        }
        button:hover {
            background-color: #45a049;
        }
        .success {
            color: #4CAF50;
        }
        .warning {
            color: #ff9800;
        }
    </style>
</head>
<body>
    <h1>清空AI聊天应用会话数据</h1>
    
    <div>
        <h3>使用说明：</h3>
        <ol>
            <li>点击下方按钮清空localStorage中的所有会话数据</li>
            <li>清空后刷新AI聊天应用页面即可看到效果</li>
        </ol>
    </div>
    
    <h3>当前localStorage中的数据：</h3>
    <pre id="currentData">正在读取...</pre>
    
    <button id="clearButton">清空所有会话数据</button>
    
    <div id="result" style="margin-top: 20px;"></div>
    
    <script>
        // 读取并显示当前localStorage数据
        function displayCurrentData() {
            const data = {};
            try {
                for (let i = 0; i < localStorage.length; i++) {
                    const key = localStorage.key(i);
                    try {
                        data[key] = JSON.parse(localStorage.getItem(key));
                    } catch (e) {
                        data[key] = localStorage.getItem(key);
                    }
                }
                document.getElementById('currentData').textContent = JSON.stringify(data, null, 2);
            } catch (error) {
                document.getElementById('currentData').textContent = '读取数据时出错: ' + error.message;
            }
        }
        
        // 清空所有相关的会话数据
        function clearAllSessionData() {
            const resultElement = document.getElementById('result');
            let itemsCleared = 0;
            
            try {
                // 获取所有键并筛选出会话相关的键
                const keysToRemove = [];
                for (let i = 0; i < localStorage.length; i++) {
                    const key = localStorage.key(i);
                    // 匹配可能的会话相关键名
                    if (key.includes('session') || key.includes('chat') || key.includes('history') || 
                        key.includes('currentChatId') || key.includes('chatHistory')) {
                        keysToRemove.push(key);
                    }
                }
                
                // 删除所有匹配的键
                keysToRemove.forEach(key => {
                    localStorage.removeItem(key);
                    itemsCleared++;
                });
                
                // 额外清理一些常见的会话相关键
                const commonKeys = ['currentChatId', 'chatHistory', 'sessions', 'recentChats'];
                commonKeys.forEach(key => {
                    if (localStorage.getItem(key) !== null) {
                        localStorage.removeItem(key);
                        itemsCleared++;
                    }
                });
                
                // 完全清空（可选）
                if (confirm('是否要完全清空所有localStorage数据？')) {
                    localStorage.clear();
                    itemsCleared = '所有';
                }
                
                resultElement.innerHTML = `<p class="success">✓ 成功清空 ${itemsCleared} 个会话相关数据项</p>`;
                displayCurrentData(); // 更新显示
            } catch (error) {
                resultElement.innerHTML = `<p class="warning">✗ 清空数据时出错: ${error.message}</p>`;
            }
        }
        
        // 初始显示
        displayCurrentData();
        
        // 绑定按钮事件
        document.getElementById('clearButton').addEventListener('click', clearAllSessionData);
    </script>
</body>
</html>